<style>
    table{ border:4px double red; border-spacing: 0;}
    th, td{ border-bottom: 1px dotted red; border-right: 1px dotted red; padding: 10px;  }
</style>

<?php
echo "<pre>";
$db=new PDO('mysql:host=localhost; dbname=db3', 'root', '12qwas');
$ps=$db->query('select * from students');
echo "字段的个数：{$ps->columnCount()}\n";
$ps=$db->prepare("select xh 学号,name 姓名,age from students where age<:age");
$ps->bindValue(':age',20, PDO::PARAM_INT);
echo "字段的个数：{$ps->columnCount()}\n";//若返回0，说明语句对象中没有包含结果集
$ps->execute();
echo "字段的个数：{$ps->columnCount()}\n";
print_r($ps->getColumnMeta(2));
echo '</pre>';
table($ps);

$ps=$db->query("show databases;");
table($ps);

function table($ps){
    $count=$ps->columnCount();//结果集的字段个数，若返回0，说明语句对象中没有包含结果集
    if($count===0)return;
    echo '<table><tr>';
    for($i=0;$i<$count;++$i){
        echo "<th>{$ps->getColumnMeta($i)['name']}</th>";
    }
    echo '</tr>';
    foreach ($ps as $r){//循环显示每一条记录的数据
        echo '<tr>';
        for($i=0;$i<$count;++$i){
            echo "<td>",$r[$i]??$r[$ps->getColumnMeta($i)['name']],"</td>";
        }
        echo '</tr>';
    }
    echo '</table>';
}